package org.scohen.juploadr.app.state;

import java.beans.XMLEncoder;
import java.io.ByteArrayOutputStream;
import java.util.Iterator;
import java.util.prefs.Preferences;

import org.scohen.juploadr.Messages;
import org.scohen.juploadr.app.ImageAttributes;
import org.scohen.juploadr.app.JUploadr;
import org.scohen.juploadr.app.tags.TagParser;
import org.scohen.juploadr.ui.Task;

public class SaveStateTask extends Task {
    private Preferences prefs = Preferences.userNodeForPackage(SaveStateTask.class);
    private JUploadr uploadr;
    private ImageAttributes currentPhoto = null;

    public SaveStateTask(JUploadr uploadr) {
        this.uploadr = uploadr;
        max = uploadr.getImageAttributes().size();
    }

    public void execute() {
        Iterator iter = uploadr.getImageAttributes().values().iterator();
        int i = min;
        while (iter.hasNext()) {
            ImageAttributes next = (ImageAttributes) iter.next();
            currentPhoto = next;
            prefs.put(i + ".path", next.getImagePath()); //$NON-NLS-1$
            prefs.put(i + ".stringTags", TagParser.toInputString(next.getTags())); //$NON-NLS-1$
            prefs.putBoolean(i + ".publiclyVisible", next.isPubliclyVisible()); //$NON-NLS-1$
            prefs.putInt(i + ".rotation", next.getRotation()); //$NON-NLS-1$
            prefs.putInt(i + ".rank", next.getRank()); //$NON-NLS-1$
            prefs.putInt(i + ".state",next.getState());
            
            if (!next.isPubliclyVisible()) {
                prefs.putBoolean(i + ".familyViewable", next.isFamilyViewable()); //$NON-NLS-1$
                prefs.putBoolean(i + ".friendsViewable", next.isFriendViewable()); //$NON-NLS-1$
            }
            if (next.getDescription() != null) {
                prefs.put(i + ".description", next.getDescription()); //$NON-NLS-1$
            }
            if (next.getTitle() != null) {
                prefs.put(i + ".title", next.getTitle()); //$NON-NLS-1$
            }
            if (!next.getPhotoSets().isEmpty()) {
                prefs.put(i + ".photoSets", objectToXML(next.getPhotoSets()));
            }
            if ( next.getLocation() != null ) {
                prefs.put(i + ".location", objectToXML(next.getLocation()));
            }
            this.updateProgress(++i);
        }
    }

    private String objectToXML(Object o) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XMLEncoder encoder = new XMLEncoder(baos);
        encoder.writeObject(o);
        encoder.close();
        return baos.toString();
    }
    public String getMessage() {
        if (currentPhoto == null) {
            return Messages.getString("juploadr.tasks.savestate.savingphotos"); //$NON-NLS-1$
        } else {
            return Messages.getString("juploadr.tasks.savestate.saving") + currentPhoto.getImagePath(); //$NON-NLS-1$
        }
    }

    public String getTitle() {
        return Messages.getString("juploadr.tasks.savestate.savingphotos"); //$NON-NLS-1$
    }

}
